package com.xunyin.modules.maincouponsend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xunyin.common.service.impl.BaseServiceImpl;
import com.xunyin.dozer.service.IGenerator;
import com.xunyin.modules.maincouponsend.domain.TMainCouponUser;
import com.xunyin.modules.maincouponsend.param.TMainCouponParam;
import com.xunyin.modules.maincouponsend.service.TMainCouponUserService;
import com.xunyin.modules.maincouponsend.service.mapper.TmainCouponUserMapper;
import com.xunyin.utils.CommonUtil;
import lombok.AllArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author admin
 * @date 2022-10-24
 */
@Service
@AllArgsConstructor
//@CacheConfig(cacheNames = "tMainCouponSend")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class TMainCouponUserServiceImpl extends BaseServiceImpl<TmainCouponUserMapper, TMainCouponUser> implements TMainCouponUserService {

    @Autowired
    private final IGenerator generator;

    @Autowired
    private final TmainCouponUserMapper tmainCouponUserMapper;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public JSONObject getNoUseCouponInfo(JSONObject jsonObject) {
        String userPhone = jsonObject.getString("userPhone");
        Integer mainId = jsonObject.getInteger("mainId");
        LambdaQueryWrapper<TMainCouponUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TMainCouponUser::getUserPhone, userPhone);
        wrapper.eq(TMainCouponUser::getStatus, 1);
        wrapper.eq(TMainCouponUser::getUseStatus, 1);
        wrapper.eq(TMainCouponUser::getMainId, mainId);
        List<TMainCouponUser> list = baseMapper.selectList(wrapper);
        //List<TmainCouponUser> list = tmainCouponUserMapper.getNoUseCouponInfo(jsonObject);
        return CommonUtil.successJson(list);
    }

    /**
     * 查询优惠券列表
     *
     * @return List<TStoreVo>
     */
    @Override
    public List<TMainCouponUser> getUserCouponList(TMainCouponParam queryParam) {
        LambdaQueryWrapper<TMainCouponUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TMainCouponUser::getUserPhone, queryParam.getUserPhone());
        wrapper.eq(TMainCouponUser::getStatus, 1);
        wrapper.eq(TMainCouponUser::getUseStatus, 1);
        if (queryParam.getMainId() != null) {
            //wrapper.eq(TMainCouponUser::getMainId, queryParam.getMainId())
            wrapper.and(wq -> wq
                    .eq(TMainCouponUser::getMainId, queryParam.getMainId())
                    .or()
                    .eq(TMainCouponUser::getDeductionType, "0"));
        }
        List<TMainCouponUser> list = baseMapper.selectList(wrapper);
        return list;
    }

}
